#ifndef RECTANGLE_H_
#define RECTANGLE_H_

struct Rectangle{
	float x1;
	float x2;
	float y1;
	float y2;

	Rectangle(){
		this->x1=0;
		this->x2=0;
		this->y1=0;
		this->y2=0;
	}

	Rectangle(float x1, float x2,float y1, float y2){
		this->x1=x1;
		this->x2=x2;
		this->y1=y1;
		this->y2=y2;
	}

	void set(float x1, float x2,float y1, float y2){
		this->x1=x1;
		this->x2=x1;
		this->y1=y1;
		this->y2=y2;
	}

	void translate(float dx,float dy){
		this->x1+=dx;
		this->x2+=dx;
		this->y1+=dy;
		this->y2+=dy;
	}

	float width(){
		return x2-x1;
	}

	float height(){
		return y2-y1;
	}

	float centerX(){
		return ((x2-x1)/2)+x1;
	}

	float centerY(){
		return ((y2-y1)/2)+y1;
	}

	void center(float x,float y){
		float width = this->width();
		float height = this->height();

		this->x1 = (x-(width/2));
		this->x2 = (x+(width/2));
		this->y1 = (y-(height/2));
		this->y2 = (y+(height/2));
	}

	bool intersect(Rectangle r2){
		return !(	this->x1 > r2.x2 || this->x2 < r2.x1 ||
					this->y2 < r2.y1 || this->y1 > r2.y2);
	}

	Rectangle join(Rectangle& r2){
		Rectangle ret(x1,x2,y1,y2);
		if(this->x1>r2.x1)
			ret.x1 = r2.x1;

		if(this->x2<r2.x2)
			ret.x2 = r2.x2;

		if(this->y1>r2.y1)
			ret.y1 = r2.y1;

		if(this->y2<r2.y2)
			ret.y2 = r2.y2;

		return ret;
	}
};

#endif /* RECTANGLE_H_ */
